package Year21_Month11.day1105;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Soultion3 {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList();
        if(root == null){
            return list;
        }
        Stack<TreeNode> stack = new Stack();
        TreeNode tre = root;
        stack.push(tre);
        while(stack != null){
            while(tre != null){
                list.add(tre.val);
                if(tre.right != null){
                    stack.push(tre.right);
                }
                stack.push(tre.left);
            }
            tre = stack.pop();
        }
        return list;
    }
}
